package vip.aster.workflow.vo;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import vip.aster.workflow.entity.WfForm;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 流程表单
 *
 * @author Aster lipian1004@163.com
 * @since 2024-04-15 14:28:44
 */
@Data
@Schema(description = "流程表单")
public class WfFormVO implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    @Schema(description = "主键")
    private String id;
    @Schema(description = "表单ID")
    private String code;
    @Schema(description = "表单名称")
    private String formName;
    @Schema(description = "版本号")
    private Integer version;
    @Schema(description = "应用ID")
    private String appId;
    @Schema(description = "流程定义ID")
    private String processDefId;
    @Schema(description = "图标")
    private String icon;
    @Schema(description = "图标颜色")
    private String iconColor;
    @Schema(description = "标签位置")
    private String labelPosition;
    @Schema(description = "标签宽度")
    private Integer labelWidth;
    @Schema(description = "数据标题")
    private List<String> dataTitle;
    @Schema(description = "设置项")
    private JSONObject settings;
    @Schema(description = "表单设置内容")
    private JSONArray formItems;
    @Schema(description = "表单全局设置")
    private JSONObject formConfig;
    @Schema(description = "流程设置内容")
    private JSONObject process;
    @Schema(description = "流程附加设置项")
    private JSONObject processConfig;
    @Schema(description = "列表设置内容")
    private JSONObject listSettings;
    @Schema(description = "排序")
    private Integer sort;
    @Schema(description = "租户ID")
    private String tenantId;
    @Schema(description = "创建时间")
    private LocalDateTime createTime;
    @Schema(description = "启用状态")
    private String status;
    @Schema(description = "备注信息")
    private String remark;

    public WfFormVO() {
        super();
    }

    public WfFormVO(WfForm entity) {
        this.id = entity.getId();
        this.code = entity.getCode();
        this.formName = entity.getFormName();
        this.version = entity.getVersion();
        this.appId = entity.getAppId();
        this.processDefId = entity.getProcessDefId();
        this.icon = entity.getIcon();
        this.iconColor = entity.getIconColor();
        this.labelPosition = entity.getLabelPosition();
        this.labelWidth = entity.getLabelWidth();
        if (!StringUtils.isBlank(entity.getDataTitle())) {
            this.dataTitle = ListUtil.toList(entity.getDataTitle().split(","));
        }
        if (!StringUtils.isBlank(entity.getSettings())) {
            this.settings = JSONObject.parseObject(entity.getSettings());
        }
        if (!StringUtils.isBlank(entity.getFormItems())) {
            this.formItems = JSONArray.parseArray(entity.getFormItems());
        }
        if (!StringUtils.isBlank(entity.getFormConfig())) {
            this.formConfig = JSONObject.parseObject(entity.getFormConfig());
        }
        if (!StringUtils.isBlank(entity.getProcess())) {
            this.process = JSONObject.parseObject(entity.getProcess());
        }
        if (!StringUtils.isBlank(entity.getProcessConfig())) {
            this.processConfig = JSONObject.parseObject(entity.getProcessConfig());
        }
        if (!StringUtils.isBlank(entity.getListSettings())) {
            this.listSettings = JSONObject.parseObject(entity.getListSettings());
        }
        this.sort = entity.getSort();
        this.tenantId = entity.getTenantId();
        this.createTime = entity.getCreateTime();
        this.status = entity.getStatus();
        this.remark = entity.getRemark();
    }

    public static List<WfFormVO> convertList(List<WfForm> source) {
        if (CollUtil.isEmpty(source)) {
            return ListUtil.empty();
        }
        List<WfFormVO> list = new ArrayList<>();
        for (WfForm entity : source) {
            list.add(new WfFormVO(entity));
        }
        return list;
    }

    public WfForm reconvert() {
        WfForm entity = new WfForm();
        entity.setId(this.id);
        entity.setCode(this.code);
        entity.setFormName(this.formName);
        entity.setVersion(this.version);
        entity.setAppId(this.appId);
        entity.setProcessDefId(this.processDefId);
        entity.setIcon(this.icon);
        entity.setIconColor(this.iconColor);
        entity.setLabelPosition(this.labelPosition);
        entity.setLabelWidth(this.labelWidth);
        if (CollUtil.isNotEmpty(this.dataTitle)) {
            entity.setDataTitle(StringUtils.join(this.dataTitle, ","));
        }
        if (this.settings != null) {
            entity.setSettings(JSONObject.toJSONString(this.settings));
        }
        if (this.formItems != null) {
            entity.setFormItems(JSONArray.toJSONString(this.formItems));
        }
        if (this.formConfig != null) {
            entity.setFormConfig(JSONObject.toJSONString(this.formConfig));
        }
        if (this.process != null) {
            entity.setProcess(JSONObject.toJSONString(this.process));
        }
        if (this.processConfig != null) {
            entity.setProcessConfig(JSONObject.toJSONString(this.processConfig));
        }
        if (this.listSettings != null) {
            entity.setListSettings(JSONObject.toJSONString(this.listSettings));
        }
        entity.setSort(this.sort);
        entity.setTenantId(this.tenantId);
        entity.setCreateTime(this.createTime);
        entity.setStatus(this.status);
        entity.setRemark(this.remark);
        return entity;
    }

}
